/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Action;

import Entity.FAQs;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Cyrax
 */
@Stateless
public class FAQ implements FAQLocal {
    @PersistenceContext(unitName = "EasyTickets-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }
    @Override
    public List<FAQs> viewAllFaq() {
        List<FAQs> list = em.createNamedQuery("FAQs.findAll").getResultList();
        return list;
    }

    @Override
    public void addFaq(FAQs au) {
        em.persist(au);
    }

    @Override
    public void updateFaq(int id, String question,String answer) {

        FAQs fa = em.find(FAQs.class, id);
        fa.setQuestion(question);
        fa.setAnswer(answer);
        em.persist(fa);

    }

    @Override
    public void deleteFaq(FAQs fa) {
        em.remove(em.merge(fa));
    }
    @Override
    public List<FAQs> searchFAQs(String question) {
        List<FAQs> ad = null;

        ad = em.createQuery("SELECT a FROM FAQs a WHERE a.question like :question").setParameter("question", "%" + question + "%").getResultList();
        System.out.println("question : " + question);


        return ad;
    }
    boolean result = false;
    @Override
    public boolean checkQuestion(String question) {
        List<FAQs> adList = null;
        adList = em.createQuery("SELECT a FROM FAQs a").getResultList();
        for (FAQs ad : adList) {
            if (ad.getQuestion().equals(question)) {
                result = true;
                break;
            } else {
                result = false;
            }
        }
        return result;
    }
}
